package com.we.risk.phoneRecord.stage2.adm;

import org.apache.flink.api.java.utils.ParameterTool;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.we.risk.phoneRecord.common.PhoneRecordDataUtils;
import com.we.risk.phoneRecord.common.Stage2GlobalSetting;
import org.apache.commons.io.FileUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.*;

import static com.we.risk.phoneRecord.common.Stage2GlobalSetting.*;
import static com.we.risk.phoneRecord.stage2.dimphone.DimPhoneRecordLabelBroadCast.OUTPUTKEY;

public class dataCompare {
    public static final String RELEASEPROP =
            "risk/phonerecord/stage2/adm_user_label_kfk_prod.properties";
    public static final String PROPSETS = "risk/phonerecord/phonerecord.properties";
    public static Logger LOG = LoggerFactory.getLogger(dataCompare.class);

    public static final String DATEFORMAT = "yyyy-MM-dd";
    public static final String INPUTKEYBY = "user_key";
    public static final String TIMESTAMP = "timestamp";
    public static final String CAPTURETIME = "capture_time";
    public static final String OUTKEYBY = "user_key";
    public static final String LABELOUTIME = "label_output_time";
    public static final String OUTJOBID = "job_id";
    public static final String JOBID = "jobID";
    public static final String PHONECALLLIST = "phoneCallList";

    public static JSONObject ProcessData(JSONObject inputJson, JSONObject userState) {
        HashMap<String, PhoneCallLabel> initMap = initLabelMap();
        SimpleDateFormat dateFormat = new SimpleDateFormat(DATEFORMAT);
        Long captureTime = inputJson.getLong(TIMESTAMP);
        String user_key = inputJson.getString(INPUTKEYBY);
        String job_id = inputJson.getString(JOBID);
        JSONArray phoneCallList = inputJson.getJSONArray(PHONECALLLIST);
        int call_cnt = phoneCallList.size();
        System.out.println("Debug call_cnt: " + call_cnt);
        /** 所有通话记录的基本信息 */
        UserBasicInfo user = new UserBasicInfo();
        /** 单个手机号的基本信息 */
        HashMap<String, PhoneCallBasicInfo> PhoneCallDetailMap = new HashMap<>();

        CalculatorBascInfo(userState, dateFormat, phoneCallList, user, PhoneCallDetailMap);

        System.out.println("Debug numbers: " + user.getUser_number());
        System.out.println("Debug total duration: " + user.getTotal_duration());
        System.out.println("Debug total Prov num: " + user.getProvNum());
        System.out.println("Debug total City num: " + user.getCityNum());

        //            LOG.warn("Debug format json:" + inputJson.toString());
        //            {
        //                int user_number = user.getUser_number();
        //                long total_duration = user.getTotal_duration();
        //                long cityNum = user.getCityNum();
        //                int provNum = user.getProvNum();
        //            }

        long startTimeOfDay = PhoneRecordDataUtils.getStartTimeOfDay(captureTime, dateFormat);
        for (int i = 0; i < call_cnt; i++) {
            JSONObject callDetail = (JSONObject) phoneCallList.get(i);
            //                System.out.println("Debug callDetail item: " + callDetail);
            String number = callDetail.getString("number");
            long callTime = Long.parseLong(callDetail.getString("date"));
            int duration = callDetail.getIntValue("duration");
            String sgtag = callDetail.getString("sgtag");
            String cstag = callDetail.getString("coname");
            String callType = callDetail.getString("type").toLowerCase();
            //                if(callType.equals("missed") &&
            //                        (callTime >
            // PhoneRecordDataUtils.timeBeforeDays(startTimeOfDay, LASTDAY1))) {
            //                    LOG.error("ProcessData: " + callDetail.toString());
            //                }if(callType.equals("missed") &&
            //                        (callTime >
            // PhoneRecordDataUtils.timeBeforeDays(startTimeOfDay, LASTDAY1))) {
            //                    LOG.error("ProcessData: " + callDetail.toString());
            //                }
            String hourGrp = callDetail.getString("hourGrp");
            String mob_prov = callDetail.getString("mob_prov");
            String mob_city = callDetail.getString("mob_city");
            Byte inbook = callDetail.getByte("inbook");
            boolean isMobile = PhoneRecordDataUtils.isMobile(number);

            PhoneCallBasicInfo phoneCallBasicInfo = PhoneCallDetailMap.get(number);

            PhoneCallObject phoneCallObject =
                    new PhoneCallObject(
                            number,
                            duration,
                            mob_prov,
                            mob_city,
                            inbook,
                            hourGrp,
                            callType,
                            PhoneRecordDataUtils.mapSougouType(sgtag),
                            PhoneRecordDataUtils.mapCsTag(cstag),
                            isMobile);

            calcuLableSection(phoneCallBasicInfo, "", initMap, phoneCallObject);

            if (callTime > PhoneRecordDataUtils.timeBeforeDays(startTimeOfDay, LASTDAY1)) {
                //                    LOG.error("ProcessData D1:" + phoneCallObject.toString());
                calcuLableSection(phoneCallBasicInfo, "d1", initMap, phoneCallObject);
            }
            if (callTime > PhoneRecordDataUtils.timeBeforeDays(startTimeOfDay, LASTDAY15)) {
                //                    System.out.println("ProcessData D15:" +
                // phoneCallObject.toString());
                //                    if(phoneCallBasicInfo.getAddrCityTag().equals("addrcity0")) {
                //                        System.out.println("##number: " + phoneCallObject.number);
                //                    }
                calcuLableSection(phoneCallBasicInfo, "d15", initMap, phoneCallObject);
            }
            if (callTime > PhoneRecordDataUtils.timeBeforeDays(startTimeOfDay, LASTDAY30)) {
                //                    LOG.error("ProcessData D30:" + phoneCallObject.toString());
                calcuLableSection(phoneCallBasicInfo, "d30", initMap, phoneCallObject);
            }
            if (callTime > PhoneRecordDataUtils.timeBeforeDays(startTimeOfDay, LASTDAY90)) {
                //                    LOG.error("ProcessData D90:" + phoneCallObject.toString());
                calcuLableSection(phoneCallBasicInfo, "d90", initMap, phoneCallObject);
            }
            if (callTime > PhoneRecordDataUtils.timeBeforeDays(startTimeOfDay, LASTDAY180)) {
                calcuLableSection(phoneCallBasicInfo, "d180", initMap, phoneCallObject);
            }
        }
        JSONObject resJson = new JSONObject();
        resJson.put(OUTKEYBY, user_key);
        resJson.put(CAPTURETIME, captureTime);
        resJson.put(OUTJOBID, job_id);
        Set<String> keySets = initMap.keySet();
        for (String key : keySets) {
            PhoneCallLabel label = initMap.get(key);
            if (key.contains(ALL + "_")) {
                key = key.replace(ALL + "_", "");
            }
            if (key.contains(ALL)) {
                key = key.replace(ALL, "");
            }

            //                {
            //                    if(key.equals("mobilecity0_bemorning")) {
            //                        if (label != null) {
            //                            LOG.error("##calcuLabelAddrEx duration: " +
            // label.duration);
            //                        } else {
            //                            LOG.error("##map get key is null ! ");
            //                        }
            //                    }
            //                }

            if (key.equals("")) {
                resJson.put("call_cnt", label.cnt);
                resJson.put("call_numbers", label.getNumbers());
                resJson.put("call_duration", label.duration);
                resJson.put("call_provs", label.provSet.size());
                resJson.put("call_citys", label.citySet.size());
            } else {
                resJson.put("call_" + key + "_cnt", label.cnt);
                resJson.put("call_" + key + "_numbers", label.getNumbers());
                resJson.put("call_" + key + "_duration", label.duration);
                if (key.startsWith(BEMORNING)
                        || key.startsWith(MORNING)
                        || key.startsWith(AFTERNOON)
                        || key.startsWith(NIGHT)
                        || key.startsWith(INCOMING)
                        || key.startsWith(OUTGOING)
                        || key.startsWith(NOBOOK)
                        || key.startsWith(BOOK)
                        || key.startsWith("d1")
                        || key.startsWith("d15")
                        || key.startsWith("d30")
                        || key.startsWith("d90")
                        || key.startsWith("d180")) {
                    resJson.put("call_" + key + "_provs", label.provSet.size());
                    resJson.put("call_" + key + "_citys", label.citySet.size());
                }
            }
        }
        long label_output_time = System.currentTimeMillis();
        resJson.put(LABELOUTIME, label_output_time);
        LOG.error("Debug resJson size: " + resJson.size());
        LOG.error("Debug resJson: " + resJson.toString());
        return resJson;
    }

    public static void CalculatorBascInfo(
            JSONObject userState,
            SimpleDateFormat dateFormat,
            JSONArray phoneCallList,
            UserBasicInfo user,
            HashMap<String, PhoneCallBasicInfo> phoneCallDetailMap) {

        int call_cnt = phoneCallList.size();
        System.out.println("call_cnt: " + call_cnt);
        for (int i = 0; i < call_cnt; i++) {
            JSONObject callJson = (JSONObject) phoneCallList.get(i);

            String number = callJson.getString("number");
            PhoneCallBasicInfo callDetail;

            if (phoneCallDetailMap.containsKey(number)) {
                callDetail = phoneCallDetailMap.get(number);
            } else {
                callDetail = new PhoneCallBasicInfo();
            }
            user.number_set.add(number);
            callDetail.cnt++;

            if (callJson.containsKey("mob_prov")) {
                String mob_prov = callJson.getString("mob_prov");
                user.prov_set.add(mob_prov);
                String state_mob_prov = userState.getString("mob_prov");
                String state_pid_prov = userState.getString("pid_prov");
                String state_ad_prov = userState.getString("ad_prov");
                //                LOG.warn("Debug CalculatorBascInfo state_mob_prov: " +
                // state_mob_prov + " state_pid_prov: " + state_pid_prov
                //                + " state_ad_prov: " + state_ad_prov);
                callDetail.mobile_prov = (byte) (mob_prov.equals(state_mob_prov) ? 1 : 0);
                callDetail.pid_prov = (byte) (mob_prov.equals(state_pid_prov) ? 1 : 0);
                callDetail.addr_prov = (byte) (mob_prov.equals(state_ad_prov) ? 1 : 0);
            } else {
                /** 非手机号解析不到地址 */
                callDetail.mobile_prov = 0;
                callDetail.pid_prov = 0;
                callDetail.addr_prov = 0;
            }

            if (callJson.containsKey("mob_city")) {
                String mob_city = callJson.getString("mob_city");
                user.city_set.add(mob_city);
                String state_mob_city = userState.getString("mob_city");
                String state_pid_city = userState.getString("pid_city");
                String state_ad_city = userState.getString("ad_city");
                //                System.out.println("Debug CalculatorBascInfo state_mob_city: " +
                // state_mob_city + " state_pid_city: " + state_pid_city
                //                        + " state_ad_city: " + state_ad_city);
                callDetail.mobile_city = (byte) (mob_city.equals(state_mob_city) ? 1 : 0);
                callDetail.pid_city = (byte) (mob_city.equals(state_pid_city) ? 1 : 0);
                callDetail.addr_city = (byte) (mob_city.equals(state_ad_city) ? 1 : 0);
            } else {
                callDetail.mobile_city = 0;
                callDetail.pid_city = 0;
                callDetail.addr_city = 0;
            }

            String callType = callJson.getString("type");
            callType = PhoneRecordDataUtils.mapCallType(callType);
            callJson.put("type", callType);

            if (callType.equals("INCOMING")) {
                user.income_cnt++;
                callDetail.income_cnt++;
            } else if (callType.equals("OUTGOING")) {
                user.outgo_cnt++;
                user.outgo_set.add(number);
                callDetail.outgo_cnt++;
            } else {
                user.bad_cnt++;
                user.bad_set.add(number);
                callDetail.bad_cnt++;
            }

            int duration = callJson.getIntValue("duration");
            user.total_duration += duration;
            callDetail.total_time += duration;
            if (duration == 0) {
                user.time0_set.add(number);
                callDetail.time0_cnt++;
            }
            if (duration < 10) {
                callDetail.time10_cnt++;
            }
            if (duration > 60) {
                user.time60_set.add(number);
                callDetail.time60_cnt++;
            }

            long callTime = Long.parseLong(callJson.getString("date"));
            String call_date = dateFormat.format(new Date(callTime));

            if (call_date != null) {
                callDetail.days++;
            }
            //            callDetail.call_days_set.add(call_date);

            String hourGroup = PhoneRecordDataUtils.getHourGroup(callTime);
            if (hourGroup.equals(Stage2GlobalSetting.BEMORNING)) {
                callJson.put("hourGrp", Stage2GlobalSetting.BEMORNING);
                callDetail.bemorning_cnt++;
            } else if (hourGroup.equals(Stage2GlobalSetting.MORNING)) {
                callJson.put("hourGrp", Stage2GlobalSetting.MORNING);
                user.morning_cnt++;
                user.morning_set.add(number);
                callDetail.morning_cnt++;
            } else if (hourGroup.equals(Stage2GlobalSetting.AFTERNOON)) {
                callJson.put("hourGrp", Stage2GlobalSetting.AFTERNOON);
                user.afternoon_set.add(number);
                callDetail.afternoon_cnt++;
            } else if (hourGroup.equals(Stage2GlobalSetting.NIGHT)) {
                callJson.put("hourGrp", Stage2GlobalSetting.NIGHT);
                user.night_set.add(number);
                callDetail.night_cnt++;
            }

            String callName = callJson.getString("name");
            Byte inBook = PhoneRecordDataUtils.inBook(callName);
            callJson.put("inbook", inBook);

            phoneCallDetailMap.put(number, callDetail);

            //            {
            //                PhoneCallBasicInfo tmpInfo = phoneCallDetailMap.get(number);
            //                LOG.warn("CalculatorBascInfo Debug res: " + tmpInfo.toString());
            //            }

            phoneCallList.set(i, callJson);
        }

        Set<String> numbers = phoneCallDetailMap.keySet();
        for (String number : numbers) {
            PhoneCallBasicInfo phoneCallBasicInfo = phoneCallDetailMap.get(number);
            phoneCallBasicInfo.score =
                    PhoneRecordDataUtils.getScore(user, phoneCallBasicInfo, number);
            //            if(number != null && (number.equals("01052207651") ||
            // number.equals("02961334480") || number.equals("075561925695"))) {
            //              if(number != null && (number.equals("18577457403"))){
            //                  int morning_number = user.getMorning_number();
            //                  LOG.error("##number: " + number + " Score: " +
            // phoneCallBasicInfo.score + " morning_number: " + morning_number);
            //            }
            phoneCallBasicInfo.formattedDetailData();
        }
    }

    private static void calcuLableSection(
            PhoneCallBasicInfo info,
            String sectSuffix,
            HashMap<String, PhoneCallLabel> initMap,
            PhoneCallObject phoneCallObject) {
        if (!sectSuffix.equals("")) {
            sectSuffix = "_" + sectSuffix;
        }
        //        if(sectSuffix.equals("_" + D90)) {
        ////            String number = phoneCallObject.getNumber();
        ////            if(number != null && number.equals("18577457403")) {
        ////                LOG.error("phoneCallObject: "+ phoneCallObject.toString());
        ////            }
        //            if(info.getScoreTag().equals("number39") && phoneCallObject.getInbook() != 1)
        // {
        ////                if(phoneCallObject.getNumber() != null) {
        //                    LOG.error("####: " + phoneCallObject.toString());
        ////                }
        //            }
        //        }
        calcuLabelEx(ALL, sectSuffix, initMap, phoneCallObject, true);
        calcuLabelEx(phoneCallObject.getHourGrp(), sectSuffix, initMap, phoneCallObject, true);
        calcuLabelEx(phoneCallObject.getCallType(), sectSuffix, initMap, phoneCallObject, true);

        if (phoneCallObject.isMobile) {
            calcuLabelEx(MOBILE, sectSuffix, initMap, phoneCallObject, false);
        } else {
            calcuLabelEx(FIX, sectSuffix, initMap, phoneCallObject, false);
        }

        calcuLabelEx(info.getCallAvgTimeTag(), sectSuffix, initMap, phoneCallObject, false);
        calcuLabelEx(info.getCallTotalTimeTag(), sectSuffix, initMap, phoneCallObject, false);
        calcuLabelEx(info.getCallCntTag(), sectSuffix, initMap, phoneCallObject, false);
        calcuLabelEx(info.getIncomeCntTag(), sectSuffix, initMap, phoneCallObject, false);
        calcuLabelEx(info.getOutgoCntTag(), sectSuffix, initMap, phoneCallObject, false);
        calcuLabelEx(info.getBadCntTag(), sectSuffix, initMap, phoneCallObject, false);
        calcuLabelEx(info.getTime0CntTag(), sectSuffix, initMap, phoneCallObject, false);
        calcuLabelEx(info.getTime10CntTag(), sectSuffix, initMap, phoneCallObject, false);
        calcuLabelEx(info.getTime60CntTag(), sectSuffix, initMap, phoneCallObject, false);
        calcuLabelEx(info.getCallDaysTag(), sectSuffix, initMap, phoneCallObject, false);
        calcuLabelEx(info.getScoreTag(), sectSuffix, initMap, phoneCallObject, false);

        calcuLabel(phoneCallObject.getSgTag() + sectSuffix, initMap, phoneCallObject, false);
        calcuLabel(phoneCallObject.getCsTag() + sectSuffix, initMap, phoneCallObject, false);

        calcuLabelAddrEx(info.getMobProvTag(), sectSuffix, initMap, phoneCallObject, false);
        calcuLabelAddrEx(info.getMobCityTag(), sectSuffix, initMap, phoneCallObject, false);
        calcuLabelAddrEx(info.getPidProvTag(), sectSuffix, initMap, phoneCallObject, false);
        calcuLabelAddrEx(info.getPidCityTag(), sectSuffix, initMap, phoneCallObject, false);
        calcuLabelAddrEx(info.getAddrProvTag(), sectSuffix, initMap, phoneCallObject, false);
        calcuLabelAddrEx(info.getAddrCityTag(), sectSuffix, initMap, phoneCallObject, false);
    }

    private static void calcuLabelEx(
            String key,
            String suffix,
            HashMap<String, PhoneCallLabel> map,
            PhoneCallObject object,
            boolean addProvCity) {
        //        key = (key.equals("") ? key : (key+"_"));
        calcuLabel(key + suffix, map, object, addProvCity);

        if (object.getInbook() == 1) {
            calcuLabel(key + "_book" + suffix, map, object, addProvCity);
        } else {
            calcuLabel(key + "_nobook" + suffix, map, object, addProvCity);
        }
    }

    /** 计算与自填地址，pid地址，手机号地址是否相同的标签 */
    private static void calcuLabelAddrEx(
            String key,
            String suffix,
            HashMap<String, PhoneCallLabel> map,
            PhoneCallObject object,
            boolean addProvCity) {
        String number = object.getNumber();

        calcuLabel(key + suffix, map, object, addProvCity);
        if (object.getHourGrp() != null) {
            calcuLabel(key + "_" + object.getHourGrp() + suffix, map, object, addProvCity);
        }

        if (object.getCallType() != null
                && (object.getCallType().equals(INCOMING)
                        || object.getCallType().equals(OUTGOING))) {
            calcuLabel(key + "_" + object.getCallType() + suffix, map, object, addProvCity);
        }

        if (object.getInbook() == 1) {
            calcuLabel(key + "_book" + suffix, map, object, addProvCity);
        } else {
            calcuLabel(key + "_nobook" + suffix, map, object, addProvCity);
        }
    }

    private static void calcuLabel(
            String key,
            HashMap<String, PhoneCallLabel> map,
            PhoneCallObject object,
            boolean addProvCity) {
        //        System.out.println("Debug calcuLabel input key: " + key);
        PhoneCallLabel phoneCallLabel = map.get(key);

        if (phoneCallLabel != null) {
            /** cnt 不计算 number 为 null 的数据 */
            String number = object.getNumber();
            long duration = object.getDuration();

            String prov = object.getProv();
            String city = object.getCity();

            if ((key.contains("prov") && prov != null) || (key.contains("city") && city != null)) {
                //                if(number != null) {
                phoneCallLabel.cnt++;
                phoneCallLabel.duration += duration;
                phoneCallLabel.addCallNumber(number);
                //                }
            }
            if (!key.contains("prov") && !key.contains("city")) {
                if (key.contains("number")) {
                    if (number != null) {
                        phoneCallLabel.cnt++;
                        phoneCallLabel.duration += duration;
                    }
                } else {
                    phoneCallLabel.cnt++;
                    phoneCallLabel.duration += duration;
                }
                phoneCallLabel.addCallNumber(number);
            }

            //            if(!key.contains(FIX)) {
            //                if (number != null) {
            //                    if ((key.contains("prov") && prov != null) ||
            // (key.contains("city") && city != null)) {
            //                        phoneCallLabel.cnt++;
            //                        phoneCallLabel.duration += duration;
            //                        phoneCallLabel.addCallNumber(number);
            //                    }
            //                    if (!key.contains("prov") && !key.contains("city")) {
            //                        phoneCallLabel.cnt++;
            //                        phoneCallLabel.duration += duration;
            //                        phoneCallLabel.addCallNumber(number);
            //                    }
            //                }
            //            } else {
            //                phoneCallLabel.cnt++;
            //                phoneCallLabel.duration += duration;
            //                phoneCallLabel.addCallNumber(number);
            //            }

            if (addProvCity) {
                if (prov != null) {
                    phoneCallLabel.provSet.add(prov);
                }
                if (city != null) {
                    phoneCallLabel.citySet.add(city);
                }
            }
        } else {
            LOG.debug("calcuLabel error input key: " + key + " enableAddr: " + addProvCity);
        }
    }

    private static HashMap<String, PhoneCallLabel> initLabelMap() {
        String[] hourGrpCalltype = {BEMORNING, MORNING, AFTERNOON, NIGHT, INCOMING, OUTGOING};

        HashMap<String, PhoneCallLabel> initMap = new HashMap<>();
        HashMap<String, PhoneCallLabel> MapBook = new HashMap<>();
        HashMap<String, PhoneCallLabel> MapD180 = new HashMap<>();
        HashMap<String, PhoneCallLabel> SgMap = new HashMap<>();
        HashMap<String, PhoneCallLabel> addrMap = new HashMap<>();

        initMap.put(ALL, new PhoneCallLabel());
        initMap.put(BEMORNING, new PhoneCallLabel());
        initMap.put(MORNING, new PhoneCallLabel());
        initMap.put(AFTERNOON, new PhoneCallLabel());
        initMap.put(NIGHT, new PhoneCallLabel());
        initMap.put(FIX, new PhoneCallLabel());
        initMap.put(MOBILE, new PhoneCallLabel());
        initMap.put(BLOCKED, new PhoneCallLabel());
        initMap.put(INCOMING, new PhoneCallLabel());
        initMap.put(OUTGOING, new PhoneCallLabel());
        initMap.put(MISSED, new PhoneCallLabel());
        initMap.put(REJECTED, new PhoneCallLabel());
        initMap.put(UNKNOWN, new PhoneCallLabel());
        initMap.put(NUMBER1, new PhoneCallLabel());
        initMap.put(NUMBER2, new PhoneCallLabel());
        initMap.put(NUMBER3, new PhoneCallLabel());
        initMap.put(NUMBER4, new PhoneCallLabel());
        initMap.put(NUMBER5, new PhoneCallLabel());
        initMap.put(NUMBER6, new PhoneCallLabel());
        initMap.put(NUMBER7, new PhoneCallLabel());
        initMap.put(NUMBER8, new PhoneCallLabel());
        initMap.put(NUMBER9, new PhoneCallLabel());
        initMap.put(NUMBER10, new PhoneCallLabel());
        initMap.put(NUMBER11, new PhoneCallLabel());
        initMap.put(NUMBER12, new PhoneCallLabel());
        initMap.put(NUMBER13, new PhoneCallLabel());
        initMap.put(NUMBER14, new PhoneCallLabel());
        initMap.put(NUMBER15, new PhoneCallLabel());
        initMap.put(NUMBER16, new PhoneCallLabel());
        initMap.put(NUMBER17, new PhoneCallLabel());
        initMap.put(NUMBER18, new PhoneCallLabel());
        initMap.put(NUMBER19, new PhoneCallLabel());
        initMap.put(NUMBER20, new PhoneCallLabel());
        initMap.put(NUMBER21, new PhoneCallLabel());
        initMap.put(NUMBER22, new PhoneCallLabel());
        initMap.put(NUMBER23, new PhoneCallLabel());
        initMap.put(NUMBER24, new PhoneCallLabel());
        initMap.put(NUMBER25, new PhoneCallLabel());
        initMap.put(NUMBER26, new PhoneCallLabel());
        initMap.put(NUMBER27, new PhoneCallLabel());
        initMap.put(NUMBER28, new PhoneCallLabel());
        initMap.put(NUMBER29, new PhoneCallLabel());
        initMap.put(NUMBER30, new PhoneCallLabel());
        initMap.put(NUMBER31, new PhoneCallLabel());
        initMap.put(NUMBER32, new PhoneCallLabel());
        initMap.put(NUMBER33, new PhoneCallLabel());
        initMap.put(NUMBER34, new PhoneCallLabel());
        initMap.put(NUMBER35, new PhoneCallLabel());
        initMap.put(NUMBER36, new PhoneCallLabel());
        initMap.put(NUMBER37, new PhoneCallLabel());
        initMap.put(NUMBER38, new PhoneCallLabel());
        initMap.put(NUMBER39, new PhoneCallLabel());
        initMap.put(NUMBER40, new PhoneCallLabel());
        initMap.put(NUMBER41, new PhoneCallLabel());
        initMap.put(NUMBER42, new PhoneCallLabel());
        initMap.put(NUMBER43, new PhoneCallLabel());

        Set<String> keySet = initMap.keySet();

        MapBook.put(NOBOOK, new PhoneCallLabel());
        MapBook.put(BOOK, new PhoneCallLabel());
        Set<String> BookSet = MapBook.keySet();

        HashMap<String, PhoneCallLabel> tmpMap = new HashMap<>();
        for (String set : BookSet) {
            for (String key : keySet) {
                //                if(key.equals("")) {
                //                    tmpMap.put(set, new PhoneCallLabel());
                //                } else {
                //                    tmpMap.put(key+"_"+set, new PhoneCallLabel());
                //                }
                tmpMap.put(key + "_" + set, new PhoneCallLabel());
            }
        }
        initMap.putAll(tmpMap);

        MapD180.put(D1, new PhoneCallLabel());
        MapD180.put(D15, new PhoneCallLabel());
        MapD180.put(D30, new PhoneCallLabel());
        MapD180.put(D90, new PhoneCallLabel());
        MapD180.put(D180, new PhoneCallLabel());
        Set<String> set180 = MapD180.keySet();
        Set<String> keySet2 = initMap.keySet();
        HashMap<String, PhoneCallLabel> tmp180Map = new HashMap<>();

        for (String set : set180) {
            for (String key : keySet2) {
                //                if(key.equals("")) {
                //                    tmp180Map.put(set, new PhoneCallLabel());
                //                } else {
                //                    tmp180Map.put(key+"_"+set, new PhoneCallLabel());
                //                }
                tmp180Map.put(key + "_" + set, new PhoneCallLabel());
            }
        }
        initMap.putAll(tmp180Map);

        SgMap.put(ELSE, new PhoneCallLabel());
        SgMap.put(BAOXIAN, new PhoneCallLabel());
        SgMap.put(DACHE, new PhoneCallLabel());
        SgMap.put(DAIKUAN, new PhoneCallLabel());
        SgMap.put(GOUWU, new PhoneCallLabel());
        SgMap.put(GUANGGAO, new PhoneCallLabel());
        SgMap.put(KEFU, new PhoneCallLabel());
        SgMap.put(KUAIDI, new PhoneCallLabel());
        SgMap.put(SAORAO, new PhoneCallLabel());
        SgMap.put(WAIMAI, new PhoneCallLabel());
        SgMap.put(XINYONGKA, new PhoneCallLabel());
        SgMap.put(YINHANG, new PhoneCallLabel());
        SgMap.put(YUNYINGSHANG, new PhoneCallLabel());
        SgMap.put(ZHONGJIE, new PhoneCallLabel());
        SgMap.put(CS, new PhoneCallLabel());

        initMap.putAll(SgMap);
        Set<String> sgSet = SgMap.keySet();
        HashMap<String, PhoneCallLabel> tmpSgMap = new HashMap<>();

        for (String set : sgSet) {
            for (String key : set180) {
                tmpSgMap.put(set + "_" + key, new PhoneCallLabel());
            }
        }
        initMap.putAll(tmpSgMap);

        addrMap.put(MOBPROV1, new PhoneCallLabel());
        addrMap.put(MOBPROV0, new PhoneCallLabel());
        addrMap.put(MOBCITY1, new PhoneCallLabel());
        addrMap.put(MOBCITY0, new PhoneCallLabel());
        addrMap.put(ADDRPROV1, new PhoneCallLabel());
        addrMap.put(ADDRPROV0, new PhoneCallLabel());
        addrMap.put(ADDRCITY1, new PhoneCallLabel());
        addrMap.put(ADDRCITY0, new PhoneCallLabel());
        addrMap.put(PIDPROV1, new PhoneCallLabel());
        addrMap.put(PIDPROV0, new PhoneCallLabel());
        addrMap.put(PIDCITY1, new PhoneCallLabel());
        addrMap.put(PIDCITY0, new PhoneCallLabel());
        initMap.putAll(addrMap);

        Set<String> addrSet = addrMap.keySet();
        HashMap<String, PhoneCallLabel> tmpAddrMap = new HashMap<>();
        for (String set : addrSet) {
            for (String key : hourGrpCalltype) {
                tmpAddrMap.put(set + "_" + key, new PhoneCallLabel());
            }
        }
        for (String set : addrSet) {
            for (String key : set180) {
                tmpAddrMap.put(set + "_" + key, new PhoneCallLabel());
            }
        }
        for (String set : addrSet) {
            for (String key : BookSet) {
                tmpAddrMap.put(set + "_" + key, new PhoneCallLabel());
            }
        }
        initMap.putAll(tmpAddrMap);

        return initMap;
    }

    private static JSONObject initPropJson(String propFile) throws IOException {
        InputStream resourceAsStream =
                dataCompare.class.getClassLoader().getResourceAsStream(propFile);
        ParameterTool reader = ParameterTool.fromPropertiesFile(resourceAsStream);
        JSONObject res = new JSONObject();
        Set<Object> keySet = reader.getProperties().keySet();
        //        System.out.println("initPropJson keySet size: " + keySet.size());
        Iterator<Object> keysIterator = keySet.iterator();
        while (keysIterator.hasNext()) {
            res.put((String) keysIterator.next(), -1);
        }
        return res;
    }

    public static void main(String[] args) throws IOException {
        try {
//            String user_input =
//                    "{\"pid_city\":\"庆阳市\",\"mob_city\":\"赣州市\",\"ad_prov\":\"甘肃省\",\"mob_prov\":\"江西省\",\"pid_prov\":\"甘肃省\",\"user_key\":\"661484a246ff34a5c9cbfe28be5416cb\",\"ad_city\":\"庆阳市\"}";
            String user_input = "{\"pid_city\":\"绥化市\",\"mob_city\":\"大庆市\",\"ad_prov\":\"黑龙江省\",\"mob_prov\":\"黑龙江省\",\"pid_prov\":\"黑龙江省\",\"user_key\":\"09dc1403a4edc2b4fd100efca0ebb834\",\"ad_city\":\"绥化市\"}";
            File file =
                    new File(
                            "/Users/jk/Downloads/风控实时标签需求/通话记录-loulingbo/data_compare/1106/phone_compare.data");
            String pr_input = FileUtils.readFileToString(file);

            JSONObject inputJson = JSONObject.parseObject(pr_input);
            String user_key = inputJson.getString(OUTPUTKEY);

            System.out.println("enter!!");
            JSONObject tmpJson = ProcessData(inputJson, JSONObject.parseObject(user_input));

            JSONObject curState = initPropJson(PROPSETS);
            curState.put(OUTPUTKEY, user_key);
            Iterator<String> iterator = tmpJson.keySet().iterator();
            while (iterator.hasNext()) {
                String key = iterator.next();
                if (!key.equals(OUTKEYBY) && curState.containsKey(key)) {
                    Object labelV = inputJson.get(key);
                    if (!curState.get(key).equals(labelV)) {
                        //                            JSONObject resJson = new JSONObject();
                        //                            resJson.put(OUTKEYBY, userKey);
                        //                            resJson.put("label_name", key);
                        //                            resJson.put("label_value", labelV);
                        curState.put(key, labelV);
                    }
                }
            }

            System.out.println(curState.toString());

        } catch (Exception e) {
            System.out.println(e.toString());
            e.printStackTrace();
        }
    }
}
